using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TuyennvLibrary.SqlClient;
using ServerApi.Models;
namespace ServerApi.Models.Menu
{
    public class TypeRole
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Weight { get; set; }


        public static string COLUMN_ID = "Id";
        public static string COLUMN_NAME = "Name";
        public static string COLUMN_WEIGHT = "Weight";


        public static string TableName = "TypeRole";
        public TypeRole()
        {
            this.Id = 0;
            this.Name = string.Empty;
            this.Weight = 0;

        }
    }
    public class TypeRoleModel : BaseSort<TypeRole>
    {
        public TypeRoleModel()
        {
            table = TypeRole.TableName;
        }
        public bool Insert(TypeRole obj)
        {
            try
            {
                DB.Insert(table, new SqlParameter[] {
					new SqlParameter(TypeRole.COLUMN_NAME,obj.Name),
					new SqlParameter(TypeRole.COLUMN_WEIGHT,obj.Weight),
                    
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool Update(TypeRole obj)
        {
            try
            {
                DB.Update(table, new SqlParameter[] {
					new SqlParameter(TypeRole.COLUMN_NAME,obj.Name),
					new SqlParameter(TypeRole.COLUMN_WEIGHT,obj.Weight),

                }, new SqlParameter[]{
					new SqlParameter(TypeRole.COLUMN_ID,obj.Id),
										 
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool Delete(int Id)
        {
            try
            {
                DB.Delete(table, new SqlParameter[] { 
					new SqlParameter(TypeRole.COLUMN_ID,Id),
										 
                });
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool DeleteMulti(string ListID)
        {
            try
            {
                if (ListID != string.Empty)
                {
                    foreach (string ID in ListID.Split(','))
                    {
                        Delete(int.Parse(ID));
                    }
                }
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<TypeRole> GetByPage(int page, int pagesize, List<TypeRole> source)
        {
            List<TypeRole> list = new List<TypeRole>();
            int index = (page - 1) * pagesize;
            int count = source.Count;
            if (pagesize > count)
                list = source;
            else if (pagesize > count - index)
                list = source.GetRange(index, count - index);
            else
                list = source.GetRange(index, pagesize);
            return list;
        }
        public List<TypeRole> GetListBy(DataTable dt)
        {
            List<TypeRole> list = new List<TypeRole>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    TypeRole obj = GetBy(dr);
                    list.Add(obj);
                }
            }
            return list;
        }
        public DataTable GetListAll()
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null,table,TypeRole.COLUMN_WEIGHT);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public TypeRole GetBy(DataRow dr)
        {
            try
            {
                TypeRole obj = new TypeRole();
                obj.Id = (dr[TypeRole.COLUMN_ID] is DBNull) ? 0 : int.Parse(dr[TypeRole.COLUMN_ID].ToString());
                obj.Name = (dr[TypeRole.COLUMN_NAME] is DBNull) ? string.Empty : dr[TypeRole.COLUMN_NAME].ToString();
                obj.Weight = (dr[TypeRole.COLUMN_WEIGHT] is DBNull) ? 0 : int.Parse(dr[TypeRole.COLUMN_WEIGHT].ToString());

                return obj;
            }
            catch (Exception)
            {
            }

            return new TypeRole();
        }
        public int GetMaxWeight()
        {
            try
            {
                string query = "select MAX(weight) from " + table;
                int rs = 0;
                rs = int.Parse(DB.ExecuteScalar(query, null).ToString());
                return rs;
            }
            catch (Exception)
            {

            }
            return 0;
        }
        public TypeRole GetBy(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    return GetBy(dr);
                }
            }
            return new TypeRole();
        }
        public TypeRole GetByID(int Id)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
					new SqlParameter(TypeRole.COLUMN_ID,Id),
										
                }, null);
                return GetBy(dt);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<TypeRole> getListMenu(int userid)
        {
            string query = "select distinct a.* from " + table + " a," + Role.TableName + " b," + RoleUser.TableName + " c," + Account.TableName + " d";
            query += " where a.id=b.typeroleid and b.id=c.roleid and d.id=c.userid and d.id=" + userid;
            query += " order by a.weight";
            DataTable dt = DB.ExecuteDataTable(query, null);
            return GetListBy(dt);
        }

        public override void SetUpWeight(TypeRole obj, int thutu)
        {
            obj.Weight = thutu;
        }

        public override int getWeight(TypeRole obj)
        {
            return obj.Weight;
        }

        public override object getId(TypeRole obj)
        {
            return obj.Id;
        }
    }
}
